package com.stwyj.sbd.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.stwyj.sbd.common.AppErrors;
import com.stwyj.sbd.config.exception.AppException;
import com.stwyj.sbd.config.exception.ResultErrorException;
import com.stwyj.sbd.dao.mapper.UserMapper;
import com.stwyj.sbd.dao.repository.UserRepository;
import com.stwyj.sbd.entity.domain.UserCollection;
import com.stwyj.sbd.entity.dto.UserDto;
import com.stwyj.sbd.entity.po.User;
import com.stwyj.sbd.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 用户service实现
 * </p>
 *
 * @author stwyj
 * @since 2018/11/30
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRepository userRepository;

    /**
     * 返回用户信息demo
     *
     * @param userDto 用户信息
     * @return 用户信息
     */
    @Override
    @Async
    public UserDto getUser(UserDto userDto) {
        try {
            Thread.sleep(3000);
            log.info("异步方法执行结束");
        } catch (InterruptedException e) {
            log.error("UserServiceImpl.getUser(),执行出现异常，异常信息为：\n", e);
        }
        return userDto;
    }

    /**
     * 更新userdemo
     */
    @Override
    @Retryable(maxAttempts = 5, include = AppException.class,backoff = @Backoff(delay = 1000L))
    public void updateUser() {
        log.info("更新用户demo执行");
        throw new AppException(1, "更新失败");
    }

    /**
     * 用户插入数据库
     *
     * @param user 用户信息
     */
    @Override
    public void insertUser(com.stwyj.sbd.entity.po.User user) {
        log.info("==============={}", user);
        for (int i = 0; i < 25; i++) {
            Integer res = userMapper.insert(user);
            log.info("插入成功状态，1：成功，0：失败，结果为：{}", res);
        }
    }

    /**
     * 返回用户信息分页对象
     *
     * @return 用户信息分页对象
     */
    @Override
    public Page<User> getUsers() {
        Page<User> res = new Page<>(1, 20, "id", false);
        return res.setRecords(userMapper.selectForPage(res));
    }

    /**
     * 分页查询usercollection集合
     *
     * @return usercollection集合
     */
    @Override
    public org.springframework.data.domain.Page<UserCollection> mongoList() {
        return userRepository.findByCreateDateAfter(new Date(), PageRequest.of(1, 10));
    }

    /**
     * 查询usercollection
     *
     * @return userCollection
     */
    @Override
    public UserCollection mongo() {
        return userRepository.findOneUserCollectionById("123");
    }

    /**
     * appException重试次数上限处理
     *
     * @param appException 捕获的异常
     * @throws ResultErrorException 错误结果异常
     */
    @Recover
    public void recover(AppException appException) throws ResultErrorException {
        log.error("UserServiceImpl.updateUser()，执行出错，异常信息为：{}", appException);
        throw new ResultErrorException(AppErrors.ERROR_UPDATE_FAIL);
    }

}
